<template>
  <div class="myDialog">
    <!-- 弹窗组件 -->
    <el-dialog
      :visible.sync="dialogFormVisible"
      :title="title"
      :before-close="handleClose"
      :close-on-click-modal="false"
      width="480px"
    >
      <el-form
        ref="ruleForm"
        :model="form"
        :rules="rules"
        label-width="120px"
      >
        <el-form-item :label="$t('AttributeCategory')" prop="category" >
          <el-radio-group v-model="form.category" size="mini">
            <el-radio-button :label="1"> {{ $t('parentAttribute') }}</el-radio-button>
            <el-radio-button :label="2">{{ $t('subAttribute') }}</el-radio-button>
          </el-radio-group>
        </el-form-item>
        <el-form-item v-show="form.category == '2'" :label="$t('parentAttribute')" prop="parentAttributeId">
          <el-select v-model="form.parentAttributeId" clearable filterable>
            <el-option
              v-for="item in parentAttributeAddList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('attributeName')" prop="attributeName">
          <el-select
            v-model="form.attributeName"
            clearable
            filterable
            @change="changeAttrOption"
          >
            <el-option
              v-for="item in attributeNameList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
          <el-input
            :style="{marginTop: '10px'}"
            v-model="form.inputAttributeName"
            :placeholder="$t('pleaseSelectOrEnterTheAttributeName')"
            @input="resetSelect"
          />
        </el-form-item>
        <el-form-item :label="$t('AttributeType')" prop="type">
          <el-input v-model="form.type" />
        </el-form-item>
        <el-form-item :label="$t('attributeValue')" prop="attributeValue">
          <el-input v-model="form.attributeValue" />
        </el-form-item>
        <el-form-item :label="$t('order')">
          <el-input v-model="form.order" onkeyup="this.value = this.value.replace(/[^\d.]/g,'')"/>
        </el-form-item>
        <el-form-item :label="$t('description')">
          <el-input v-model="form.description" maxlength="200" show-word-limit type="textarea" rows="3" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button icon="el-icon-circle-close" @click="handleClose">{{
          $t("cancel")
        }}</el-button>
        <el-button
          v-btnThrottle="doSave"
          type="primary"
          icon="el-icon-receiving"
        >{{ $t('save') }}</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>
<script>
import crudJob from '@/api/system/attributes'
import mySetting from '@/mixins/mySetting'
export default {
  name: 'MyDialog',
  mixins: [mySetting],
  data() {
    return {
      title: '',
      form: {
        inputAttributeName: '',
        attributeName: ''
      },
      attributeNameList: [],
      parentAttributeAddList: [],
      dialogFormVisible: false,
      rules: {
        category: [{ required: true, message: this.$t('pleaseSelectTheAttributeCategory'), trigger: 'change' }],
        type: [{ required: true, message: this.$t('pleaseEnterTheAttributeType'), trigger: 'blur' }],
        attributeValue: [
          { required: true, message: this.$t('pleaseEnterAttributeValue'), trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    changeAttrOption() {
      this.form.inputAttributeName = ''
    },
    resetSelect(value) {
      if (value) {
        this.form.attributeName = ''
      }
    },
    // 新增数据
    doSave() {
      this.$refs['ruleForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        if (this.form.id) {
          // 存在id则是更新
          crudJob.edit({ id: this.form.id, ...this.form }).then((res) => {
            this.$betterTips(res, this.handleUpdate, this.handleClose)
          })
        } else {
          // 不存在id则是新增
          crudJob.add(this.form).then((res) => {
            this.$betterTips(res, this.handleUpdate, this.handleClose)
          })
        }
      })
    },
    // 打开弹窗
    openDialog(row) {
      if (row && row.id) {
        this.title = this.$t('modify')
        crudJob.get({ id: row.id }).then(res => {
          this.form = res.data
        })
      } else {
        this.title = this.$t('add')
      }
      this.dialogFormVisible = true
      crudJob
        .attributenamelist()
        .then((res) => {
          this.attributeNameList = res.data
        })
      crudJob
        .parentattributeaddlist()
        .then((res) => {
          this.parentAttributeAddList = res.data
        })
      this.$nextTick(() => {
        this.$refs['ruleForm'].clearValidate()
      })
    },
    handleClose() {
      Object.assign(this.$data, this.$options.data.call(this))
    },
    handleUpdate() {
      this.$emit('initList')
    }
  }
}
</script>
<style lang="scss" scoped>
</style>
